import { getStorage, setStorage } from './storge'
import { defaultAbilityList, abilityDetail } from './data'
import { defaultOperationList } from './ability'

const listKey = 'abilityListKey'
const operationKey = 'operationListKey'

export const getAbilityList = async (params?: any) => {
  const abilityList = getStorage(listKey)
  if (abilityList) {
    return abilityList as any
  } else {
    setAbilityList(defaultAbilityList)
    return defaultAbilityList as any
  }
}

export const setAbilityList = async (list: any[]) => {
  setStorage(listKey, list)
}

export const getOperationList = async (params?: any) => {
  if (params) {
    delete params.pageNum
    delete params.pageSize
  }
  const abilityList = getStorage(operationKey) || defaultOperationList
  let list = []
  const keys = Object.keys(params || {})
  if (!keys?.length) {
    return {
      list: abilityList,
      total: abilityList.length
    }
  }
  keys?.forEach((item) => {
    list = list.concat(abilityList.filter((i) => i[item] == params[item]))
  })
  console.log(list)
  return {
    list: list,
    total: list.length
  }
}

export const editOperation = async (id: string | number, from: Object) => {
  return new Promise((resolve, reject) => {
    setTimeout(async () => {
      try {
        const data = await getOperationList()
        const list = data?.list
        const row = list?.find((item) => item.abilityId == id)
        if (!row) {
          reject(new Error('未找到该行数据'))
        }
        Object.keys(from).forEach((item) => {
          row[item] = from[item]
        })
        setOperation(list)
        resolve(true)
      } catch (err) {
        console.log(err)
        reject(err)
      }
    }, 200)
  })
}

export const setOperation = async (list: any[]) => {
  setStorage(operationKey, list)
}

export const getAbilityDetail = async (id?: any): Promise<any | null> => {
  if (!id || !abilityDetail[id]) return null

  const tmpRes: any = { ...abilityDetail[id] }
  const abilityList = getStorage(operationKey) || defaultOperationList
  const ability = abilityList.find((item) => item.abilityId === id)
  // console.log('ability', ability)

  if (ability) {
    tmpRes.code = ability.code
    let reviewList = []
    if (ability.bereviewedStatus === '已评审') {
      reviewList.push({ status: ability.bereviewedStatus, comment: ability.approveComment })
      if (ability.betestedStatus === '已测试') {
        reviewList.push({
          status: ability.betestedStatus,
          comment: ability.testApproveComment,
          testFile1: ability.testFile1 || '',
          testFileName1: ability.testFileName1 || '',
          testFile2: ability.testFile2 || '',
          testFileName2: ability.testFileName2 || '',
          testFile3: ability.testFile3 || '',
          testFileName3: ability.testFileName3 || ''
        })
        if (ability.onStatus === '已上架') {
          reviewList.push({
            status: ability.onStatus,
            comment: ability.email
          })
          if (ability.offStatus === '已下架') {
            reviewList.push({
              status: ability.offStatus,
              comment: ability.offReason
            })
          }
        }
      }
    }
    tmpRes.reviewList = reviewList
  }

  return tmpRes
}

// export const setOperation = async (list: any[]) => {
//   setStorage(operationKey, list)
// }
